home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2010 Summer - Disc 1 / WN_Ete2010_CD1.iso / Onglet5 / Weezo / Weezo setup.exe / {code_appDir} / www / local / pluginsManagement.php < prev    next >
PHP Script  |  2010-05-19  |  16KB  |  450 lines

  1. <?php
  2. /**
  3.  * Plugins management script
  4.  *
  5.  *
  6.  *
  7.  * PHP version 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category   NA
  16.  * @package    NA
  17.  * @author     Nicolas Bruley / Peer 2 World <contact@weezo.net>
  18.  * @copyright  2005-2009 Nicolas Bruley / Peer 2 World
  19.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  20.  * @version    CVS: $Id:$
  21.  * @link       http://www.weezo.net
  22.  * @since      File available since Release 2.0.0
  23.  */
  24.  
  25. define('PLUGINS_VERSIONS_UPDATE_TIME',21600); // Time between 2 refresh
  26. define('WINDOW_REDUCEDSIZE','"600x128"'); // reduced (download) size window
  27. define('WINDOW_FULLSIZE','"600x600"'); // full size window
  28.  
  29. /**
  30.  * @desc Display plugin box
  31.  *
  32.  * @param array $plugin
  33.  * @param string $key
  34.  */
  35. function pluginBox($plugin,$key){
  36.     if(isset($plugin['installedVersion']) && @$plugin['isUpdateAvailable']) $extraClass='installUpdated';
  37.     elseif(isset($plugin['installedVersion'])) $extraClass='installInstalled';
  38.     else $extraClass='installNew';
  39.  
  40.     echo outDivFrame('frame2 '.$extraClass);
  41.  
  42.     // Preview image
  43.     echo '<img style="width:64;height:48;float:right;margin-left:2em;margin-bottom:1em;margin-right:1em;margin-top:0.5em;visibility:hidden;border:1px solid #888" onload="fade(this)" alt="" src="'.$plugin['resourcePreviewImage'].'" onmouseover="tooltip(this,\'<img src=\\\''.$plugin['resourcePreviewImage'].'\\\'>\')">';
  44.  
  45.     // Icon and name
  46.     echo '<div class="frame2Header"><img src="'.$plugin['resourceIconSmall'].'" style="vertical-align:middle;margin-right:1em">'.cfUTF8Encode($plugin['name']).'</div>';
  47.  
  48.  
  49.     // Description
  50.     echo '<div style="text-align:justify" title="'.((strlen(@$plugin['description'])>200)?(str_ireplace('<br>',' ',@$plugin['description'])):'').'">';
  51.     if(isset($plugin['description'])) echo (cfStrTruncate($plugin['description'],200)); else echo cfCaption('genNoDescription');
  52.     echo '</div>';
  53.     echo '<br clear="all">';
  54.  
  55.     // Require MySQL
  56.     if(@$plugin['requireMySQL'] && !cfMySQLInstalled()) echo '<div class="extendedWarning">'.outImage(outIcon('warning'),false,false,'float:left').'<center>'.cfCaption('extMySQLRequired').'</center></div>';
  57.  
  58.     $l=$r='';
  59.  
  60.     // Plugin already installed
  61.     if(isset($plugin['installedVersion'])){
  62.         // Update available
  63.         if(@$plugin['isUpdateAvailable']) {
  64.             $l.=outImage(outIcon('info2'),false,false,'margin-top:8px;margin-right:0.5em;display:inline');
  65.             $l.='<fieldset style="margin-right:2em">';
  66.             // Installed version
  67.             $l.=cfCaption('versionInstalled').cfCaption('genSeparator').(($plugin['installedVersion'])?$plugin['installedVersion']:'?');
  68.             // New available version
  69.             $l.='<div class="warning">'.cfCaption('versionLatest').cfCaption('genSeparator').$plugin['latestVersion'].'</div>';
  70.             $l.='</fieldset>';
  71.             $l.=outButton(cfCaption('genUpdate'),"javascript:update('".$key."')",outIcon('dl2'));
  72.         }
  73.         else{
  74.             // Green colored installed version
  75.             $l.='<span style="color:#3A3">'.cfCaption('versionInstalled').cfCaption('genSeparator').(($plugin['installedVersion'])?$plugin['installedVersion']:'?').'</span>';
  76.         }
  77.  
  78.         // Remove resource button
  79.         if($plugin['uninstallAllowed']) $r.=outButton(cfCaption('genDelete'),"javascript:uninstall('".$key."','".addslashes($plugin['name'])."',".$plugin['installedNb'].")",outIcon('del'));
  80.     }
  81.  
  82.     // Plugin not installed
  83.     else{
  84.         $l.=cfCaption('versionLatest').cfCaption('genSeparator').$plugin['latestVersion'];
  85.         $r.=outButton(cfCaption('genDownload').' / '.cfCaption('genInstall'),"javascript:install('".$key."')",outIcon('dl2'));
  86.     }
  87.  
  88.     // Insert into footer
  89.     echo outFrameFooterTable('frame2Footer','<b>'.$l.'</b>',$r);
  90.     echo "</div>\n";
  91. }
  92.  
  93. /**
  94.  * Compare current version and latest, return true if latest>current
  95.  *
  96.  * @param string $current: current version number, separated by dots
  97.  * @param string $latest: latest version number, separated by dots
  98.  * @return unknown
  99.  */
  100. function isUpdateAvailable($current,$latest){
  101.     if($latest===$current) return false;
  102.     $current=explode('.',$current);
  103.     $latest=explode('.',$latest);
  104.     foreach ($latest as $pos=>$value) if($value>@$current[$pos]) return true;
  105.     return false;
  106. }
  107.  
  108. /**
  109.  * @desc return all installed and available plugins, and for installed ones, indicate if a newer version is available
  110.  *
  111.  * @return array...
  112.  */
  113. function pluginGetList(){
  114.  
  115.     $plugins=array();
  116.     $lng=cfGGetVar('language');
  117.     $updatesAvailable=0;
  118.  
  119.     // Count number of installations of plugins
  120.     $createdResources=array();
  121.     foreach (cfMGetVar('weezoResourcesList') as $id=>$file){
  122.         if(cfArrayItem(cfMGetVar('weezoResData'.$id),'type')=='misc' || cfArrayItem(cfMGetVar('weezoResData'.$id),'subType')=='phpMyAdmin') @$createdResources[strtolower(cfArrayItem(cfMGetVar('weezoResData'.$id),'subType'))]++;
  123.     }
  124.  
  125.     /**
  126.      * Download latest versions list
  127.      */
  128.     if(!is_array($latestVersions=cfMGetVar('weezoPluginsVersion')) || @$latestVersions['time']<(time()-PLUGINS_VERSIONS_UPDATE_TIME)) {
  129.         $pluginsVersion=@cfSocketHTTPRequest(DOWNLOAD_SITE.'/plugins/versions');
  130.  
  131.         file_put_contents(cfAppTempDir().'/tmp_plugins',$pluginsVersion);
  132.         $latestVersions=parse_ini_file(cfAppTempDir().'/tmp_plugins',true);
  133.         @unlink(cfAppTempDir().'/tmp_plugins');
  134.  
  135.         // If download OK
  136.         if(is_array($latestVersions)) {
  137.             // Set "version" file download time
  138.             $latestVersions['time']=time();
  139.             // Remove spaces from keys
  140.             foreach ($latestVersions as $key=>$value) if(strpos($key,' ')){
  141.                 $value['name']=$key;
  142.                 $latestVersions[str_replace(' ','',$key)]=$value;
  143.                 unset($latestVersions[$key]);
  144.             }
  145.         }
  146.         cfMSetVar('weezoPluginsVersion',$latestVersions);
  147.     }
  148.  
  149.     /**
  150.      * Check installed plugins versions and number of created resources
  151.      */
  152.  
  153.     // Add MySQL/phpMyAdmin
  154.     if($MySQL=cfArrayItem(cfMGetVar('weezoResourcesDefinitions'),'administration','phpMyAdmin')){
  155.         $MySQL=array('MySQL'=>$MySQL);
  156.     }
  157.     else
  158.         $MySQL=array();
  159.     $resourcesUsingMySQL=0;
  160.  
  161.     // Add ffmpeg
  162.     if(cfFfmpegVersion()=='GPL')
  163.         $plugins['ffmpeggpl']=array(
  164.             'name'=>'FFmpeg GPL',
  165.             'installedNb'=>-1,
  166.             'uninstallAllowed'=>false,
  167.             'installedVersion'=>(file_exists(cfAppBinDir().'/ffmpeg/version.txt'))?file_get_contents(cfAppBinDir().'/ffmpeg/version.txt'):((filesize(cfAppBinDir().'/ffmpeg/weezoFfmpeg.exe')==5965312)?0.5:0),
  168.             'resourceIconSmall'=>outIcon('ffmpeg')
  169.         );
  170.  
  171.     // Browse installed resources
  172.     $installed=cfArrayItem(cfMGetVar('weezoResourcesDefinitions'),'misc'); if(!is_array($installed)) $installed=array();
  173.     foreach ($MySQL+$installed as $key=>$resDefId){
  174.         $resData=cfMGetVar($resDefId);
  175.         $describerData=@$resData['describer'];
  176.  
  177.         // Plugin's name
  178.         $plugins[strtolower($key)]['name']=$key;
  179.         $key=strtolower($key);
  180.  
  181.         if(isset($describerData[$lng]['name'])) $plugins[$key]['name']=$describerData[$lng]['name'];
  182.         elseif(isset($describerData['en']['name'])) $plugins[$key]['name']=$describerData['en']['name'];
  183.  
  184.         // Installed version
  185.         $plugins[$key]['installedVersion']=(isset($describerData['version']))?$describerData['version']:0;
  186.  
  187.         // Number of resources using this plugin
  188.         $plugins[$key]['installedNb']=(int)@$createdResources[$key];
  189.  
  190.         // Description (utf8 encoded)
  191.         if(isset($describerData[$lng]['description'])) $plugins[$key]['description']=($describerData[$lng]['description']);
  192.         elseif(isset($describerData['en']['description'])) $plugins[$key]['description']=($describerData['en']['description']);
  193.  
  194.         // Preview image URL
  195.         $plugins[$key]['resourcePreviewImage']=$resData['resourcePreviewImage'];
  196.  
  197.         // 32x32 icon URL
  198.         $plugins[$key]['resourceIconSmall']=$resData['resourceIconSmall'];
  199.  
  200.         // Extension requires MySQL to be installed
  201.         if(@$describerData['requireMySQL']) {
  202.             $plugins[$key]['requireMySQL']=true;
  203.             $resourcesUsingMySQL+=$plugins[$key]['installedNb'];
  204.         }
  205.  
  206.         // Plugin may be uninstalled
  207.         $plugins[$key]['uninstallAllowed']=true;
  208.  
  209.         // MySQL mods
  210.         if($key=='mysql') {
  211.             $plugins[$key]['name']='MySQL / phpMyAdmin';
  212.             $plugins[$key]['installedNb']=(int)@$createdResources['phpmyadmin'];
  213.             if($plugins[$key]['installedVersion']) $plugins[$key]['resourceIconSmall']='/res/administration/phpMyAdmin/combinedIcon.png';
  214.         }
  215.     }
  216.     if(isset($plugins['mysql'])) $plugins['mysql']['installedNb']+=$resourcesUsingMySQL;
  217.  
  218.     /**
  219.      * Combine information from non-installed plugins, and add latest version info
  220.      */
  221.     foreach ($latestVersions as $key=>$latestVersion) if($key!=='time') {
  222.         if(isset($latestVersion['name'])) $name=$latestVersion['name']; else $name=$key;
  223.  
  224.         $key=strtolower($key);
  225.  
  226.         // Latest available version
  227.         $plugins[$key]['latestVersion']=$latestVersion['version'];
  228.         if($plugins[$key]['isUpdateAvailable']=isUpdateAvailable(@$plugins[$key]['installedVersion'],$latestVersion['version'])) $updatesAvailable++;
  229.  
  230.         // Fill name
  231.         if(!isset($plugins[$key]['name'])) $plugins[$key]['name']=$name;
  232.  
  233.         // Fill description
  234.         if(!isset($plugins[$key]['description']) && isset($latestVersion['description']) && is_array($latestVersion['description'])) {
  235.             foreach ($latestVersion['description'] as $k=>$desc){
  236.                 if(substr($desc,0,2)==$lng || substr($desc,0,2)=='en'){
  237.                     $plugins[$key]['description']=str_replace('-<-','(',str_replace('->-',')',substr($desc,2))); // ( and ) couldn't be parsed by parse_ini_string
  238.                     break;
  239.                 }
  240.             }
  241.         }
  242.  
  243.         // URL to thumbnail
  244.         if(!isset($plugins[$key]['resourcePreviewImage'])) $plugins[$key]['resourcePreviewImage']=DOWNLOAD_SITE.'/plugins/'.$key.'/preview.jpg';
  245.  
  246.         // URL to icon
  247.         if(!isset($plugins[$key]['resourceIconSmall'])) $plugins[$key]['resourceIconSmall']=DOWNLOAD_SITE.'/plugins/'.$key.'/resourceIconSmall.png';
  248.  
  249.         // Download URL
  250.         $plugins[$key]['downloadURL']=DOWNLOAD_SITE.'/plugins/'.$key.'/'.$name.'.weezoResource';
  251.     }
  252.     return $plugins;
  253. }
  254.  
  255. /**
  256.  * @desc Display plugin download progress page, download plugin, and pass downloaded file to UI to finish installation
  257.  *
  258.  * @param string $key: plugin name
  259.  */
  260. function pluginDownload($key){
  261.     require_once(INCLUDE_DIR.'miscFunctions.php');
  262.  
  263.     $key=str_replace(' ','',$key);
  264.  
  265.     // Remove previous file
  266.     @unlink($localFile=cfAppTempDir().'/'.$key.'.weezoResource');
  267.  
  268.     // Insert download page
  269.     cfInsertHEAD(false);
  270.     echo '<meta icon="plugins"></meta>';
  271.     echo '<meta wintitle="'.APPLICATION_NAME.'"></meta>';
  272. ?>
  273. <script type="text/javascript">
  274. function cancel(){
  275.     wl.UICommand('fadeSize',<?php echo WINDOW_FULLSIZE;?>);
  276.     dgi('downloadDiv').style.display="none";
  277.     wl.goURL("<?php echo $_SERVER['PHP_SELF'];?>");
  278. }
  279. </script>
  280. </head>
  281. <body>
  282. <?php
  283.     echo '</head>';
  284.     echo '<body style="margin-top:0;margin-bottom:0">';
  285.     echo outDivFrame('frame2','id="downloadDiv" style="margin-bottom:0"');
  286.     echo '<div class="frame2Header">'.outImageIcon('fi/icoPlugin').cfCaption('logFileDownload',$key).'</div><br><center>';
  287.     // Download file
  288.     $result=mfMonitoredDownload(DOWNLOAD_SITE.'/download.php?plugin='.$key,$localFile,array('cancelHref'=>'javascript:cancel()','appendHTML'=>' '));
  289.     echo '</center></div>';
  290.  
  291.     if($result) {
  292. ?>
  293. <script type="text/javascript">
  294. wl.UICommand('installPluginFromFile','<?php echo $localFile;?>');
  295. </script>
  296. <?php
  297.     }
  298.     else{
  299.         echo outDivFrame('frame2');
  300. ?>
  301. <div class="frame2Header"><?php echo outImageIcon('warning').cfCaption('genError');?></div>
  302. <br>
  303. <center><?php echo outButton(cfCaption('genCancel'),'javascript:cancel()',outIcon('backRed'));?></center><br> 
  304. <script type="text/javascript">
  305. dgi('downloadDiv').style.display="none";
  306. wl.UICommand('fadeSize',<?php echo (WINDOW_REDUCEDSIZE-10);?>);
  307. </script>
  308.  
  309. <?php
  310.     }
  311.     die('</body>');
  312. }
  313.  
  314. /**
  315.  * @desc Uninstall plugin
  316.  *
  317.  * @param string $key
  318.  */
  319. function pluginUninstall($key){
  320.     require_once(INCLUDE_DIR.'resourceConfigFunctions.php');
  321.  
  322.     // Ignore user abort to ensure resource is completely removed
  323.     ignore_user_abort(true);
  324.  
  325.     if($key=='mysql'&& cfMySQLInstalled() && @cfProcessExists('mysqld.exe')) {
  326.         foreach (@win32_ps_list_procs() as $proc) if(strtolower(basename(@$proc['exe']))=='mysqld.exe') {
  327.             customTerminateProcess(@$proc['pid']);
  328.             sleep(1);
  329.             break;
  330.         }
  331.         // Remove bin directory - try 3 times as it may take a little time for file to be released after being killed
  332.         $i=3; while($i--) {
  333.             cfUnlinkDir(cfAppDataRootDir().'/MySQL/bin');
  334.             if(file_exists(cfAppDataRootDir().'/MySQL/bin/mysqld.exe')) sleep(1); else break;
  335.         }
  336.         cfUnlinkDir(cfAppDataRootDir().'/MySQL/scripts');
  337.         cfUnlinkDir(cfAppDataRootDir().'/MySQL/share');
  338.         cfUnlinkDir(cfAppDocRoot().'/res/administration/phpMyAdmin');
  339.         foreach (cfGlob(cfAppDataRootDir().'/MySQL/*.*') as $cfn) if(basename($cfn)!='MySQL.ini') @unlink($cfn);
  340.  
  341.         $type='administration';
  342.         $subType='phpmyadmin';
  343.  
  344.     }
  345.     else{
  346.         // Remove plugin scripts
  347.         cfUnlinkDir(cfAppDocRoot().'/res/misc/'.$key);
  348.  
  349.         $type='misc';
  350.         $subType=$key;
  351.     }
  352.  
  353.     // Remove resource definition from list
  354.     $weezoResourcesDefinitions=cfMGetVar('weezoResourcesDefinitions');
  355.     foreach ($weezoResourcesDefinitions[$type] as $sT=>$val) {
  356.         if(strtolower($sT)==strtolower($subType)) {
  357.             cfMUnsetVar($val);
  358.             unset($weezoResourcesDefinitions[$type][$sT]);
  359.             cfMSetVar('weezoResourcesDefinitions',$weezoResourcesDefinitions);
  360.             break;
  361.         }
  362.     }
  363.  
  364.     // Remove linked resources
  365.     foreach(cfMGetVar('weezoResourcesList') as $id=>$configFilename){
  366.         $resData=cfMGetVar('weezoResData'.$id);
  367.         if(strtolower($resData['type'])==$type && strtolower($resData['subType'])==$subType){
  368.             rcDeleteResource($configFilename);
  369.         }
  370.     }
  371.  
  372.     // Refresh UI resources tab
  373.     cfServerSendCmd('updateResourcesPanel',false,false);
  374. }
  375.  
  376.  
  377. if(!cfIsInApp()) die('unauthorized access');
  378.  
  379. require_once(INCLUDE_DIR.'outputFunctions.php');
  380.  
  381. //cfMUnsetVar('weezoPluginsVersion');
  382.  
  383. /**
  384.  ***************************************************************************************************************************
  385.  * Proceed to download/installation
  386.  ***************************************************************************************************************************
  387.  */
  388. if(isset($_POST['download'])) pluginDownload($_POST['download']);
  389. if(isset($_GET['download'])) pluginDownload($_GET['download']);
  390.  
  391. // Uninstall plugin
  392. if(isset($_POST['uninstall'])) pluginUninstall($_POST['uninstall']);
  393.  
  394.  
  395. /**
  396.  ***************************************************************************************************************************
  397.  * Prepare installed extensions and available extensions data
  398.  ***************************************************************************************************************************
  399.  */
  400. $plugins=pluginGetList();
  401.  
  402.  
  403. /**
  404.  ***************************************************************************************************************************
  405.  * Display plugins management page
  406.  ***************************************************************************************************************************
  407.  */
  408. cfInsertHEAD(false);
  409. echo '<meta icon="plugins"></meta>';
  410. echo '<meta wintitle="'.APPLICATION_NAME.'"></meta>';
  411. echo '</head>';
  412. echo '<body>';
  413. echo cfScriptLink('wz_dragdrop.js');
  414.  
  415. echo outDivFrame('frame1');
  416. echo outFrameHeaderTable('frame1Header',outImageIcon('plugins').cfCaption('resMisc'),outButton(cfCaption('configNewResource').' ('.cfCaption('genFile').')','javascript:installFile()',outIcon('fi/icoPlugin')));
  417.  
  418. // Display plugins ordered by 1)updated 2)installed 3)others
  419. foreach ($plugins as $key=>$plugin) if(isset($plugin['installedVersion']) && @$plugin['isUpdateAvailable']) pluginBox($plugin,$key);
  420. foreach ($plugins as $key=>$plugin) if(isset($plugin['installedVersion']) && !@$plugin['isUpdateAvailable']) pluginBox($plugin,$key);
  421. foreach ($plugins as $key=>$plugin) if(!isset($plugin['installedVersion'])) pluginBox($plugin,$key);
  422.  
  423.  
  424.  
  425. echo "</div>\n"; // frame1
  426. //cfVarDump($plugins);?>
  427. <script type="text/javascript">
  428. function installFile(){
  429.     wl.UICommand('installPluginFromFile');
  430. }
  431. function update(key){
  432.     install(key);
  433. }
  434. function install(key){
  435.     wl.UICommand('fadeSize',<?php echo WINDOW_REDUCEDSIZE;?>);
  436.     wl.postData({'download':key})
  437. }
  438. function uninstall(key,name,nbInstalled){
  439.     var capt='<?php echo addslashes(cfCaption('confirmUninstall','PLUGINNAME'));?>';
  440.     var capt2='<?php echo addslashes(cfCaption('pluginDeleteConfirm','NBRESOURCES'));?>';
  441.     capt=capt.replace(/PLUGINNAME/,name);
  442.     if(nbInstalled>0) capt+="\n\n"+capt2.replace(/NBRESOURCES/,nbInstalled);
  443.  
  444.     if(confirm(capt)) {
  445.         maskShow(true,false,true);
  446.         wl.postData({'uninstall':key})
  447.     }
  448. }
  449. SET_DHTML()
  450. </script>